﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Yipusoft.TownshipEnterprise.BLL;
using Yipusoft.TownshipEnterprise.Model;
using System.Collections.Generic;
public partial class TownshipEnterprise_AllList : System.Web.UI.Page
{
    static string deptid=string.Empty;
    static string flag = string.Empty;

    protected void Page_Load(object sender, EventArgs e)
    {
        //this.DptDepartment.SelectedIndexChanged += new EventHandler(DptDepartment_SelectedIndexChanged);

        if (!IsPostBack)
        {
            try
            {

                deptid = Request.QueryString["DepartmentID"].ToString();
                flag = Request.QueryString["Flag"].ToString();

                 if (deptid == null || flag == null)
                 {
                     Response.Write("<script language=javascript>alert('参数获取错误1')</script>");
                     return;
                 }
                 else
                 {
                     //
                     Bind2(0);
                 }
            }
            catch
            {
                Response.Write("<script language=javascript>alert('参数获取错误!')</script>");
                return;
            }
        }

    }

    protected void ListPageContral1_PageSizeChange(int pageSize, int PageIndex)
    {
        //this.GvwList.PageIndex = PageIndex;
        Bind2(PageIndex);
    }
    
    //查询
    protected void BtnSearch_Click(object sender, EventArgs e)
    {
        Bind2(1);
    }
    /// <summary>
    /// 查询
    /// </summary>
    private void Bind2(int pageindex)
    {
        string strSql = string.Empty;

        //strSql += "  Left(DepartmentID,4) = '" + deptid.Substring(0, 4) + "'";
        strSql += " DepartmentID like '" + deptid + "%'";
        DBCls db = new DBCls();
        String TdTitle ="";
        if(deptid.Length>4)
            TdTitle = db.QueryDataTable("SELECT distinct  dpt_as  FROM View_TownDept WHERE DepartmentID like '" + deptid + "%'", "dpt_szd").Rows[0][0].ToString().Trim();
        else
            TdTitle = db.QueryDataTable("SELECT distinct  dpt_szd  FROM View_TownDept WHERE DepartmentID like '" + deptid + "%'", "dpt_szd").Rows[0][0].ToString().Trim();
        if (flag == "2")
        {
            strSql += " and FBType='乡镇四有消防队'";

            lblTitle.Text = TdTitle+" 乡镇四有消防队";
        }
        else if (flag == "1")
        {
            strSql += " and FBType='企事业专职消防队'";
            lblTitle.Text = TdTitle + " 企事业专职消防队";
        }
        else
        {
            lblTitle.Text = TdTitle + " 全部消防队";
        }
       
        List<TownBasicInfo> list = TownBasic.GetList(strSql, ListPageContral1.PageSize, pageindex);
        if (list != null)
        {
            this.ListPageContral1.ItemCount = TownBasic.GetCount(strSql);
            this.GvwList.DataSource = list;
            this.GvwList.DataBind();
        }

    }

    public string GetDepartmentType(object objID)
    {
        return objID.ToString();

    }
    public string GetStationLevel(object objID)
    {
        return objID.ToString();

        //if (objID == null)
        //    return "";
        //Yipusoft.Common.Model.CodeItemInfo info = Yipusoft.Common.BLL.CodeItem.GetCodeItem(objID.ToString(), "XFZDJ");
        //return info != null ? info.ItemName : "";
    }
    /// <summary>
    /// 
    /// </summary>
    /// <param name="objID"></param>
    /// <returns></returns>
    public string GetStationType(object objID)
    {
        return objID.ToString();
    }
    public string GetVehicleID(object objID)
    {
        return objID.ToString();
    }
    protected void GvwList_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label lblID = e.Row.FindControl("lblID") as Label;
            lblID.Text = Convert.ToString(GvwList.PageSize * ListPageContral1.CurrentPageIndex + (e.Row.RowIndex + 1));
            //当鼠标放上去的时候 先保存当前行的背景颜色 并给附一颜色 
            e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#F5F5F5',this.style.fontWeight='';");
            //当鼠标离开的时候 将背景颜色还原的以前的颜色 
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor,this.style.fontWeight='';");


            e.Row.Cells[e.Row.Cells.Count - 1].Attributes.Add("onclick", "return confirm('确定删除吗？');");
        }
    }
    protected void GvwList_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        try
        {
            int num = Convert.ToInt32(e.CommandArgument);//得行号
            Guid id = new Guid(this.GvwList.DataKeys[num]["Id"].ToString());//得主键值         

            string s = e.CommandName.ToString();
            //删除
            if (s == "Delete")
            {
                if (TownBasic.Delete(id))
                {
                    Response.Write("<script language=javascript>alert(' 删除成功!')</script>");
                }
            }
        }
        catch (Exception ex)
        {
            Response.Write("<script language=javascript>alert('操作失败！gridview-rowcommand错误！" + ex.Message + "！')</script>");
        }
    }
    protected void GvwList_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        Bind2(ListPageContral1.CurrentPageIndex);
    }
}
